package com.tsing.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tsing.common.result.PageResult;
import com.tsing.common.result.Result;
import com.tsing.mapper.AppUserMapper;
import com.tsing.mapper.impl.AppUserMapperImpl;
import com.tsing.pojo.dto.TlUserDTO;
import com.tsing.pojo.vo.AppUserVO;
import com.tsing.pojo.vo.ArticleDetailsVO;
import com.tsing.service.AppUserService;
import com.tsing.service.ArticleService;
import com.tsing.service.impl.AppUserServiceImpl;
import com.tsing.service.impl.ArticleServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * @auth 卢孝宇
 * @date 2023/12/13 8:38
 */
public class UserController {
    private final AppUserService appUserService = new AppUserServiceImpl();

    //查询个人用户的信息
    public Result<AppUserVO> list(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer userId = jsonObject.getInteger("userId");
        userId = (userId != null && userId > 0) ? userId : 1;


        AppUserVO result = appUserService.list(userId);

        return Result.success(result);
    }

    //查询个人用户收藏的作品
    public PageResult<ArticleDetailsVO> postList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer userId = (Integer) jsonObject.get("userId");
        userId = (userId != null && userId > 0) ? userId : 1;
        Integer page = (Integer) jsonObject.get("page");
        page = (page != null && page > 0) ? page : 1;
        Integer size = (Integer) jsonObject.get("size");
        size = (size != null && size > 0) ? size : 10;


        PageResult<ArticleDetailsVO> result = appUserService.collectList(userId, page, size);

        return result;
    }

    //查询个人用户笔记的作品
    public PageResult<ArticleDetailsVO> noteList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer userId = (Integer) jsonObject.get("userId");
        userId = (userId != null && userId > 0) ? userId : 1;
        Integer page = (Integer) jsonObject.get("page");
        page = (page != null && page > 0) ? page : 1;
        Integer size = (Integer) jsonObject.get("size");
        size = (size != null && size > 0) ? size : 10;


        PageResult<ArticleDetailsVO> result = appUserService.noteList(userId, page, size);

        return result;
    }

    //修改个人资料
    public Result<Integer> updateUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Long userId =  jsonObject.getLong("userId");
        userId = (userId != null && userId > 0) ? userId : null;

        String  userName=jsonObject.getString("userName");
        userName = (userName != null && !userName.isEmpty()) ? userName : null;

        String userImage = jsonObject.getString("userImage");
        userImage = (userImage != null && !userImage.isEmpty()) ? userImage : null;

        Long bookiPhone = jsonObject.getLong("bookiPhone");
        bookiPhone = (bookiPhone != null && bookiPhone > 0) ? bookiPhone : null;

        String signature = jsonObject.getString("signature");
        signature = (signature != null && !signature.isEmpty()) ? signature : null;

        String introduction =  jsonObject.getString("introduction");
        introduction = (introduction != null && !introduction.isEmpty()) ? introduction : null;

        Long sex =  jsonObject.getLong("sex");

        Long status = jsonObject.getLong("status");
        status = (status != null) ? status : 1;

        TlUserDTO tlUserDTO = new TlUserDTO();
        tlUserDTO.setUserId(userId);
        tlUserDTO.setUserImage(userImage);
        tlUserDTO.setUsername(userName);
        tlUserDTO.setIntroduction(introduction);
        tlUserDTO.setSex(sex);
        tlUserDTO.setSignature(signature);
        tlUserDTO.setBookiPhone(bookiPhone);
        tlUserDTO.setStatus(status);

        Integer result = appUserService.updata(tlUserDTO);

        return Result.success(result);
    }
}
